package com.ceres.device.web.controller;

import com.ceres.device.pojo.Device;
import com.ceres.device.pojo.OrderDevice;
import com.ceres.device.web.service.DeviceWebService;
import com.ceres.entity.Result;
import com.ceres.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/device")
public class DeviceWebController {

    @Autowired
    private DeviceWebService deviceWebService;

    /**
     * 根据设备名称查询所有
     * @param name
     * @return
     */
    @GetMapping("/search")
    public Result findByName(@RequestParam String name){
        try {
            List<Device> byName = deviceWebService.findByName(name);
            return new Result(true,StatusCode.OK,byName);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    /**
     * 根据设备类型进行分类查询
     * @param type
     * @return
     */
    @GetMapping("/sort")
    public Result findByType(@RequestParam String type){
        try{
            List<Device> byType = deviceWebService.findByType(type);
            return new Result(true,StatusCode.OK,byType);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    /**
     * 查询所有设备
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        try{
            List<Device> list = deviceWebService.findAll();
            return new Result(true,StatusCode.OK,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.OK);
        }
    }


}
